#include "StdAfx.h"
#include "CCServer.h"
#include "ServerConfig.h"
#include "TinyCCDBMgr.h"
#include "TinyServerDefine.h"

using namespace DBLayer;

CCCServer::CCCServer(void)
{
}

CCCServer::~CCCServer(void)
{
}

bool CCCServer::init(void)
{
	m_config = new CServerConfig();
		
	if(m_config == NULL)
		return false;
	g_Logger.selectlogType("server");
	g_Logger.log(debug1,"CCServer init begin...");
	m_config->readConfigFile(m_config->getConfigFileName());

	//init data from db

	g_TinyCCDBMgr.init(DBTYPE_MYSQL,m_config->m_DataBaseConnStr);
	g_TinyTCPIF.setTCPServer(&m_TCPServer);
	g_DevicePool.init();
	g_Logger.log(debug1,"CCServer init end");

	return true;
}

void CCCServer::ProcessReceivedMsg(CTinyMsg& inMsg,CTinyMsg& OutMsg)
{
	//log msg
	g_Logger.log(debug1,inMsg);

	switch(inMsg.getMsgIndex())
	{
	case CLIENTMGR:
		g_TinyCCClientMgr.ProcessReceivedMsg(inMsg,OutMsg);
		break;
	case CLIENT_ACTION:
		g_TinyCCClientMgr.ProcessReceivedMsg(inMsg,OutMsg);
		break;
	case VOICECARD_ACTION:
		g_DevicePool.ProcessReceivedMsg(inMsg,OutMsg);
		break;
	default:
		break;
	};

	OutMsg.setSourceIP(m_config->m_ModuleIP.c_str());
	OutMsg.setSourcePort(m_config->m_ModulePort);
	g_Logger.log(debug1,OutMsg);
	return ;
}